/**
 * 
 */
package org.xlab.familystore.db;

import java.sql.SQLException;
import java.util.Date;
import java.util.HashMap;

import org.xlab.familystore.domain.FirstCategory;
import org.xlab.familystore.domain.SecondCategory;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.util.Log;

import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper;
import com.j256.ormlite.dao.BaseDaoImpl;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.support.ConnectionSource;
import com.j256.ormlite.table.TableUtils;

/**
 * @author makubex
 * 
 */
public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
	// name of the database file for your application -- change to something
	// appropriate for your app
	private static final String DATABASE_NAME = "family.store.db";
	// any time you make changes to your database objects, you may have to
	// increase the database version
	private static final int DATABASE_VERSION = 1;
	private HashMap<Class<?>, Dao<?, ?>> daoMap = new HashMap<Class<?>, Dao<?, ?>>();

	public DatabaseHelper(Context context, String databaseName,
			CursorFactory factory, int databaseVersion) {
		super(context, databaseName, factory, databaseVersion);
	}

	public DatabaseHelper(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
	}

	/**
	 * @see com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper#onCreate(android.database.sqlite.SQLiteDatabase,
	 *      com.j256.ormlite.support.ConnectionSource)
	 */
	@Override
	public void onCreate(SQLiteDatabase arg0, ConnectionSource connectionSource) {
		try {
			Log.i(DatabaseHelper.class.getName(), "onCreate");
			TableUtils.createTable(connectionSource, FirstCategory.class);
			TableUtils.createTable(connectionSource, SecondCategory.class);

			@SuppressWarnings("unchecked")
			Dao<FirstCategory, Integer> dao1 = (Dao<FirstCategory, Integer>) this
					.getDao(FirstCategory.class);
			FirstCategory simple = new FirstCategory();
			simple.setCreatedTime(new Date());
			simple.setLastUpdatedTime(new Date());
			simple.setName("Cate1");
			dao1.create(simple);

			@SuppressWarnings("unchecked")
			Dao<SecondCategory, Integer> dao2 = (Dao<SecondCategory, Integer>) this
					.getDao(SecondCategory.class);
			SecondCategory simple2 = new SecondCategory();
			simple2.setCreatedTime(new Date());
			simple2.setLastUpdatedTime(new Date());
			simple2.setName("Cate1_1");
			simple2.setFirstCategory(simple);
			dao2.create(simple2);
			simple2 = new SecondCategory();
			simple2.setCreatedTime(new Date());
			simple2.setLastUpdatedTime(new Date());
			simple2.setName("Cate1_2");
			simple2.setFirstCategory(simple);
			dao2.create(simple2);
		} catch (SQLException e) {
			Log.e(DatabaseHelper.class.getName(), "Can't create database", e);
			throw new RuntimeException(e);
		}
	}

	public Dao<?, ?> getDao(Class<?> clazz) throws SQLException {
		if (daoMap.get(clazz) == null) {
			Dao<?, ?> dao = BaseDaoImpl.createDao(super.getConnectionSource(),
					clazz);
			daoMap.put(clazz, dao);
			return dao;
		} else
			return daoMap.get(clazz);
	}

	/**
	 * @see com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper#onUpgrade(android
	 *      .database.sqlite.SQLiteDatabase,
	 *      com.j256.ormlite.support.ConnectionSource, int, int)
	 */
	@Override
	public void onUpgrade(SQLiteDatabase db, ConnectionSource connectionSource,
			int arg2, int arg3) {
		try {
			Log.i(DatabaseHelper.class.getName(), "onUpgrade");
			TableUtils.dropTable(connectionSource, FirstCategory.class, true);
			// after we drop the old databases, we create the new ones
			onCreate(db, connectionSource);
		} catch (SQLException e) {
			Log.e(DatabaseHelper.class.getName(), "Can't drop databases", e);
			throw new RuntimeException(e);
		}
	}

}
